package cms.web.action.quartz;

import javax.annotation.Resource;

import cms.utils.PathUtil;
import cms.utils.newimgmerge.ImgLiteUtil;
import cms.web.action.queue.QueueListCache;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cms.web.action.fileSystem.FileManage;
import cms.web.action.lucene.QuestionIndexManage;
import cms.web.action.lucene.TopicIndexManage;
import cms.web.action.template.LayoutManage;
import cms.web.action.thumbnail.ThumbnailManage;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 * 定时任务类
 *
 */
@Component("task")
public class Task {
	
	@Resource FileManage fileManage;
	
	@Resource ThumbnailManage thumbnailManage;
	@Resource TopicIndexManage topicIndexManage;
	@Resource QuestionIndexManage questionIndexManage;
	@Resource LayoutManage layoutManage;
	@Resource QueueListCache queueListCache;
	@Value("${bbs-local.img-file-size-trigger}")
	private int maxSize;

	private static final Logger logger = LogManager.getLogger(Task.class);

	/**
	 * 话题全文索引
	 */
	@Async("taskExecutor_topicIndexManage_topicIndex")
	@Scheduled(cron = "0 0/1 * * * ?")//每隔1分钟执行一次
	public void topicIndex() {
		topicIndexManage.updateTopicIndex();
	}
	/**
	 * 问题全文索引
	 */
	@Async("taskExecutor_questionIndexManage_questionIndex")
	@Scheduled(cron = "0 0/1 * * * ?")//每隔1分钟执行一次
	public void questionIndex() {
		questionIndexManage.updateQuestionIndex();
	}
	
	/**
	 * 处理缩略图
	 */
	@Async("taskExecutor_thumbnailManage_treatmentThumbnail")
	@Scheduled(cron = "0 0/10 * * * ?")//每隔10分钟运行一次
	public void treatmentThumbnail() {
		thumbnailManage.treatmentThumbnail();
	}
	
	/**
	 * 删除无效的上传临时文件
	 */
	@Async("taskExecutor_fileManage_deleteInvalidFile")
	@Scheduled(cron = "0 50 0/2 * * ?")//每隔两小时的50分运行一次
	public void deleteInvalidFile() {
		fileManage.deleteInvalidFile();
	}
	
	/**
	 * 定时处理布局路径
	 * 异步任务配置cms.web.action.AsyncConfig.java
	 */
	@Async("taskExecutor_layoutManage_timerProcessLayoutUrl")
	@Scheduled(cron = "0/3 * * * * ?")//每隔3秒执行一次
	public void timerProcessLayoutUrl() {
		layoutManage.timerProcessLayoutUrl();
	}

	/**
	 * 定时处理  轻量化任务  不允许异步执行
	 * 异步任务配置cms.web.action.AsyncConfig.java
	 */
//	@Async("taskExecutor_liteLinkedList_liteImg")
	@Scheduled(cron = "49 0/1 * * * ?")//每分钟执行一次
	public void liteImg() {
		//获取到topic的缓存List
		LinkedList<List<String>> currTopicList = queueListCache.getImgLiteQueue();
		while (CollectionUtils.isNotEmpty(currTopicList)) {
			List<String> imgFileStrList = currTopicList.removeFirst();
			String pathPrefix = PathUtil.defaultExternalDirectory()+ File.separator+"file"+File.separator+"topic"+File.separator;
			for (String imgFileStr : imgFileStrList) {
				if(StringUtils.isEmpty(imgFileStr)) {
					continue;
				}
				//如果是gif动态图，那就跳过
				String[] split = imgFileStr.split("\\.");
				if("gif".equalsIgnoreCase(split[split.length - 1])) {
					continue;
				}
				File currImgFile = new File(pathPrefix + imgFileStr);
				if(currImgFile.length() > this.maxSize * 1024) {
					//调用轻量化
					try{
						ImgLiteUtil.liteSingleImg(currImgFile, currImgFile, 900);
						logger.info("图片轻量化成功：{}", currImgFile.getAbsolutePath());
					} catch (Exception e) {
						logger.info("图片轻量化失败：{}", currImgFile.getAbsolutePath());
						e.printStackTrace();
					}
				}
			}
		}
	}

	public static void main(String[] args) {
		//测试用
		String testStr = "C://345.t/123.gif";
		String[] split = testStr.split(".");
		System.out.println(split.length);
	}
}
